home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLSR106.ARJ / IGETLINE.CC < prev    next >
C/C++ Source or Header  |  1992-03-29  |  4KB  |  121 lines

  1. //    This is part of the iostream library, providing input/output for C++.
  2. //    Copyright (C) 1992 Per Bothner.
  3. //
  4. //    This library is free software; you can redistribute it and/or
  5. //    modify it under the terms of the GNU Library General Public
  6. //    License as published by the Free Software Foundation; either
  7. //    version 2 of the License, or (at your option) any later version.
  8. //
  9. //    This library is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. //    Library General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU Library General Public
  15. //    License along with this library; if not, write to the Free
  16. //    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #include "iostream.h"
  19.  
  20. istream& istream::getline(char* buf, int len, char delim)
  21. {
  22.     _gcount = 0;
  23.     if (ipfx1()) {
  24.     long count = rdbuf()->sgetline(buf, len, delim, 0);
  25.     _gcount = count;
  26.     if (count <= 0 || count == len-1)
  27.         set(ios::failbit);
  28.     }
  29.     return *this;
  30. }
  31.  
  32. istream& istream::get(char* buf, int len, char delim)
  33. {
  34.     _gcount = 0;
  35.     if (ipfx1()) {
  36.     long count = rdbuf()->sgetline(buf, len, delim, -1);
  37.     if (count <= 0)
  38.         set(ios::failbit);
  39.     else
  40.         _gcount = count;
  41.     }
  42.     return *this;
  43. }
  44.  
  45. //    This is part of the iostream library, providing input/output for C++.
  46. //    Copyright (C) 1992 Free Software Foundation.
  47. //
  48. //    This library is free software; you can redistribute it and/or
  49. //    modify it under the terms of the GNU Library General Public
  50. //    License as published by the Free Software Foundation; either
  51. //    version 2 of the License, or (at your option) any later version.
  52. //
  53. //    This library is distributed in the hope that it will be useful,
  54. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  55. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  56. //    Library General Public License for more details.
  57. //
  58. //    You should have received a copy of the GNU Library General Public
  59. //    License along with this library; if not, write to the Free
  60. //    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  61.  
  62. // from Doug Schmidt
  63.  
  64. #define CHUNK_SIZE 512
  65.  
  66. /* Reads an arbitrarily long input line terminated by a user-specified
  67.    TERMINATOR.  Super-nifty trick using recursion avoids unnecessary calls
  68.    to NEW! */
  69.  
  70. char *_sb_readline (streambuf *sb, long& total, char terminator) 
  71. {
  72.     char buf[CHUNK_SIZE+1];
  73.     char *ptr;
  74.     int ch;
  75.     
  76.     long count = sb->sgetline(buf, CHUNK_SIZE+1, terminator, -1);
  77.     if (count == EOF)
  78.     return NULL;
  79.     ch = sb->sbumpc();
  80.     long old_total = total;
  81.     total += count;
  82.     if (ch != EOF && ch != terminator) {
  83.     ptr = _sb_readline(sb, total, terminator);
  84.     if (ptr)
  85.         memcpy(ptr + old_total, buf, count);
  86.     return ptr;
  87.     }
  88.     
  89.     if (ptr = new char[total+1]) {
  90.     ptr[total] = '\0';
  91.     memcpy(ptr + total - count, buf, count);
  92.     return ptr;
  93.     } 
  94.     else 
  95.     return NULL;
  96. }
  97.  
  98. /* Reads an arbitrarily long input line terminated by TERMINATOR.
  99.    This routine allocates its own memory, so the user should
  100.    only supply the address of a (char *). */
  101.  
  102. istream& istream::gets(char **s, char delim /* = '\n' */)
  103. {
  104.     if (ipfx1()) {
  105.     long size = 0;
  106.     streambuf *sb = rdbuf();
  107.     *s = _sb_readline (sb, size, delim);
  108.     _gcount = *s ? size : 0;
  109.     if (sb->_flags & _S_EOF_SEEN) {
  110.         set(ios::eofbit);
  111.         if (_gcount == 0)
  112.         set(ios::failbit);
  113.     }
  114.     }
  115.     else {
  116.     _gcount = 0;
  117.     *s = NULL;
  118.     }
  119.     return *this;
  120. }
  121.